package cn.wolfcode.luowowo.webSite.controller;

import cn.wolfcode.luowowo.cache.domain.ShopStatisVO;
import cn.wolfcode.luowowo.cache.service.IShopStatisRedisService;
import cn.wolfcode.luowowo.common.domain.ShopComment;
import cn.wolfcode.luowowo.common.query.ShopCommentObject;
import cn.wolfcode.luowowo.common.service.IShopCommentService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.search.query.ShopSearchQueryObject;
import cn.wolfcode.luowowo.search.service.IShopDetailTemplateSearchService;
import cn.wolfcode.luowowo.search.service.IShopStrategyTemplateSearchService;
import cn.wolfcode.luowowo.shop.domain.ShopDetail;
import cn.wolfcode.luowowo.member.domain.UserMoney;
import cn.wolfcode.luowowo.shop.service.IShopDetailService;
import cn.wolfcode.luowowo.shop.service.IShopRollService;
import cn.wolfcode.luowowo.shop.service.IShopStrategyService;
import cn.wolfcode.luowowo.member.service.IUserMoneyService;
import cn.wolfcode.luowowo.webSite.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 积分商城
 */
@Controller
@RequestMapping("/shop")
public class ShopController {
    @Reference
    private IShopRollService shopRollService;
    @Reference
    private IShopDetailService shopDetailService;
    @Reference
    private IShopStrategyTemplateSearchService shopStrategyTemplateSearchService;
    @Reference
    private IShopStrategyService shopStrategyService;
    @Reference
    private IShopDetailTemplateSearchService shopDetailTemplateSearchService;
    @Reference
    private IShopStatisRedisService shopStatisRedisService;
    @Reference
    private IUserMoneyService userMoneyService;
    @Reference
    private IShopCommentService shopCommentService;



    @RequestMapping("/saveComment")
    @ResponseBody
    public Object commentAdd(ShopComment shopComment, @UserParam UserInfo userInfo) {

        //把用户的数据注入到攻略评论对象里
        shopComment.setCity(userInfo.getCity());
        shopComment.setHeadUrl(userInfo.getHeadImgUrl());
        shopComment.setLevel(userInfo.getLevel());
        shopComment.setUsername(userInfo.getNickname());
        shopComment.setUserId(userInfo.getId());

        shopCommentService.saveOrUpdate(shopComment);  //保存评论

//        //增加评论数
//        strategyStatisService.replynumIncrease(shopComment.getDetailId(), 1);
//
//        //获取当前游记的评论数
//        int replynum = strategyStatisService.getStrategyStatisVO(shopComment.getDetailId()).getReplynum();
//
//        //热门排行分数加1
//        strategyStatisService.addHotScroes(RedisKeys.STRATEGY_STATIS_VO.join(shopComment.getDetailId().toString()),1);

        return AjaxResult.SUCCESS;
    }

    //评论显示
    @RequestMapping("/comment")
    public String comment(Model model, @ModelAttribute("qo") ShopCommentObject qo) {
        Page page = shopCommentService.query(qo);

        model.addAttribute("page", page);

        return "/shop/commentTpl";
    }

    //攻略评论的点赞
    @RequestMapping("/commentThumbUp")
    @ResponseBody
    public Object commentThumbUp(String toid, Long fromid) {
        shopCommentService.thumbUp(toid, fromid);

        return AjaxResult.SUCCESS;
    }


    //显示积分商品推荐前五宣传
    @RequestMapping("")
    public String index(Model model) {
       //显示积分商品推荐前五宣传
        model.addAttribute("shop", shopRollService.getTop5());
       //显示精品推荐3个
        model.addAttribute("shopDetails", shopDetailService.getTop3());
        return "/shop/index";
    }


    @RequestMapping("list")
    public String prop(Model model) {

        int item=3;
        for (int i = 1; i <= item; i++) {
            //显示左侧导航栏
            List<Map<String, Object>> maps = shopStrategyTemplateSearchService.selectByThemeId(i);
            model.addAttribute("shop"+i, maps);
        }

        //显示推荐热门目录图片滚动
        model.addAttribute("commends", shopStrategyService.getHot());

        List<ShopStatisVO> hotTop10 = shopStatisRedisService.getHotTop10();
        //兑换销量榜
        model.addAttribute("roll", hotTop10);
       //菜单栏
        model.addAttribute("themes", shopStrategyTemplateSearchService.selectByParentIdIsNull());


        return "/shop/list";
    }

    @RequestMapping("/searchPage")
    public String searchPage(Model model,ShopSearchQueryObject qo) {
        Page page = shopDetailTemplateSearchService.query(qo);
        model.addAttribute("page", page);
        return "/shop/searchPageTpl";
    }


    //通过id找到下的商品
    @RequestMapping("/detail")
    public String detail(Model model,Long id,@UserParam UserInfo userInfo) {
        if (userInfo!=null){
            UserMoney userMoney = userMoneyService.selectById(userInfo.getId());
            userInfo.setUserMoney(userMoney);
            model.addAttribute("userInfo", userInfo);
        }
        ShopDetail detail = shopDetailService.selectByDetailId(id);
        model.addAttribute("shop", detail);
        return "/shop/detail";
    }

//购买商品

    @RequestMapping("/pay")
    @ResponseBody
    public AjaxResult shop(Long sid, Integer pay, @UserParam UserInfo userInfo){
        if (userInfo==null){
            return new AjaxResult(false,"请先登录");
        }else {
            UserMoney userMoney = userMoneyService.selectById(userInfo.getId());
            if (userMoney.getIntegral()<pay){
                return new AjaxResult(false,"密币不足");
            }
        }
        return AjaxResult.SUCCESS;
    }
}

